#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : Chestnut
# @time    : 2025/6/27 08:39
# @function: What can I do.
# @version : V1
import sys
import tkinter as tk
from io import StringIO


class StdoutRedirector:
    def __init__(self, text_widget):
        self.text_widget = text_widget

    def write(self, string):
        self.text_widget.insert(tk.END, string)
        self.text_widget.see(tk.END)

    def flush(self):
        pass


root = tk.Tk()
root.title("控制台输出到 Text")

text = tk.Text(root)
text.pack()
root.mainloop()

sys.stdout = StdoutRedirector(text)